package com.cash.loan.modular.cash.controller;

import com.cash.loan.core.base.controller.BaseController;
import com.cash.loan.modular.cash.model.LoanCate;
import com.cash.loan.modular.cash.result.ServerResponse;
import com.cash.loan.modular.cash.service.CateService;
import com.cash.loan.modular.cash.transfer.CateDto;
import com.cash.loan.modular.cash.transfer.UpdateCateDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 类别管理
 * @author Administrator
 * @Date 2018-09-29 15:52
 */
@Controller
@RequestMapping(value = "/cash/cate")
@Api(description = "产品类别管理")
public class CateController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(CateController.class);

    private static final String PREFIX = "/cash/cate";

    @Autowired
    private CateService cateService;

    /**
     * 进入list页面
     * @return
     */
    @RequestMapping(value = "/listPage")
    public String listPage() {
        return PREFIX + "/cate_list.html";
    }

    @ApiOperation("类别列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public ServerResponse list() {
        List<LoanCate> loanCateList = cateService.list();
        return ServerResponse.getSuccess("success",loanCateList);
    }

    @ApiOperation("新增类别")
    @PostMapping(value = "/add")
    @ResponseBody
    public ServerResponse add(@RequestBody CateDto cate, BindingResult result) {
        if (result.hasErrors()) {
            return ServerResponse.getErrorMessage(result.getAllErrors().toString());
        }
        Boolean addRes = cateService.add(cate);
        if(addRes) {
            return ServerResponse.getSuccess("新增成功");
        } else {
            return ServerResponse.getErrorMessage("新增失败");
        }
    }

    @ApiOperation("详情")
    @GetMapping(value = "/detail")
    @ResponseBody
    public ServerResponse detail(@RequestParam(value = "id") Integer id) {
        LoanCate loanCate = cateService.detail(id);
        return ServerResponse.getSuccess("success",loanCate);
    }

    @ApiOperation("修改类别")
    @PostMapping(value = "/update")
    @ResponseBody
    public ServerResponse update(@RequestBody  UpdateCateDto cate,BindingResult result) {
        if (result.hasErrors()) {
            return ServerResponse.getErrorMessage(result.getAllErrors().toString());
        }
        Boolean updateRes = cateService.update(cate);
        if(updateRes) {
            return ServerResponse.getSuccess("修改成功");
        } else {
            return ServerResponse.getErrorMessage("修改失败");
        }
    }

    @ApiOperation("删除类别")
    @PostMapping(value = "/delete")
    @ResponseBody
    public ServerResponse delete(@RequestParam(value = "id") Integer id) {
        Boolean delRes = cateService.delete(id);
        if(delRes) {
            return ServerResponse.getSuccess("删除成功");
        }
        return ServerResponse.getErrorMessage("删除失败");
    }

    @ApiOperation("绑定类别和产品")
    @PostMapping(value = "/bindProductAndCate")
    @ResponseBody
    public ServerResponse bindProductAndCate(@RequestBody Map<String,Object> cateProductId) {
        Integer productId = Integer.valueOf((String) cateProductId.get("productId"));
        List<Integer> cateIdList = (List<Integer>) cateProductId.get("cateIdList");
        if(productId == null || cateIdList.isEmpty()) {
            return ServerResponse.getErrorMessage("数据未能完整接受");
        }
        Boolean bindResult = cateService.bindCateAndProduct(productId,cateIdList);
        if(bindResult) {
            return ServerResponse.getSuccess("绑定成功");
        }
        return ServerResponse.getErrorMessage("绑定失败");
    }
}
